package com.warmlight.voicepacket.ui.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.BottomSheetBehavior;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.Add2ListActivity;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.SplashActivity;
import com.warmlight.voicepacket.adapter.PlayDetalAdapter;
import com.warmlight.voicepacket.base.BaseActivity;
import com.warmlight.voicepacket.data.PlaydeatalData;
import com.warmlight.voicepacket.data.RecommendFeed;
import com.warmlight.voicepacket.data.eventbus.AudioCompleted;
import com.warmlight.voicepacket.data.eventbus.CollectMoreAudio;
import com.warmlight.voicepacket.data.eventbus.CollectSingleAudio;
import com.warmlight.voicepacket.data.eventbus.NotifyMyPlaylist;
import com.warmlight.voicepacket.data.eventbus.NotifyPlayDetal;
import com.warmlight.voicepacket.data.eventbus.PlayerServiceData;
import com.warmlight.voicepacket.dbdata.DBFloatPlayListDetail;
import com.warmlight.voicepacket.dbdata.DBFloatPlaylist;
import com.warmlight.voicepacket.dbmanager.DBFloatPlayListDaoHelp;
import com.warmlight.voicepacket.fragment.CopyShareFragment;
import com.warmlight.voicepacket.fragment.CustomBottomSheetDialogFragment;
import com.warmlight.voicepacket.http.Callback;
import com.warmlight.voicepacket.http.HttpClient;
import com.warmlight.voicepacket.http.HttpConfig;
import com.warmlight.voicepacket.ui.Add2NetListActivity;
import com.warmlight.voicepacket.utils.ClickLog;
import com.warmlight.voicepacket.utils.GlideCornerTransform;
import com.warmlight.voicepacket.utils.MobClickHelper;
import com.warmlight.voicepacket.utils.SPUtils;
import com.warmlight.voicepacket.widget.FooterListView;
import com.zhuge.analysis.stat.ZhugeSDK;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

import okhttp3.Call;

import static com.warmlight.voicepacket.data.eventbus.PlayerServiceData.PLAY;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.AUDIO_ID;
import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_ID;


/**
 * 网络播单详情
 */
public class OtherPlayDetailAcitivity extends BaseActivity implements View.OnClickListener{
    private FooterListView footerListView;
    private View vHead;
    private LinearLayout ll_header;
    private PlayDetalAdapter adapter=null;
    private LinearLayout ll_play_all, ll_share ,ll_back;
    private TextView tv_desc,tv_header_title,tv_header_titlename;
    private ImageView iv_playimg;
    private  boolean ISBODAN=true;//播单页面
    private  PlaydeatalData.DataBean.ShareBean sharedata;
    private  List<PlaydeatalData.DataBean.ListBean> listBean;
    private  List<PlaydeatalData.DataBean.ListBean> datas=new ArrayList<>();
    public static final int UPDATE = 0x1;
    private int page=1;
    PlaydeatalData playdeatalData;
    private DBFloatPlaylist mPlaylistData;
    private List<DBFloatPlayListDetail> mAudios;
    private boolean isManager;
    private boolean isSelectAll;
    private TextView tv_cancel;
    private TextView tv_select_all;
    private ImageView iv_back;
    private LinearLayout ll_manager;
    private RelativeLayout rl_cover_add;
    private RelativeLayout rl_cover_delete;
    private LinearLayout ll_start_manager;
    private String type;
    private String pushtitle;
    private boolean isLoadAll = false;
    private String uid;
    private String nickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playdeatal_cover_list);

        if (getIntent().getStringExtra(LIST_ID)!=null){
            type = getIntent().getStringExtra("type");
            if (type == null){
                type = "";
            }
            pushtitle = getIntent().getStringExtra("pushtitle");
            if (pushtitle == null){
                pushtitle = "";
            }

            uid = getIntent().getStringExtra("uid");
            if (TextUtils.isEmpty(uid)){
                uid = "";
            }
            nickname = getIntent().getStringExtra("nickname");

            ISBODAN=false;
            initView();
            initData();
        }else {
            finish();
//            ISBODAN=true;
//            initView();
//            Intent intent = getIntent();
//            mPlaylistData = (DBFloatPlaylist) intent.getSerializableExtra(LOCAL);
//            tv_header_title.setText(mPlaylistData.getList_name());
////            mAudios = DBHelper.getInstance(getThis()).queryAudiosByListId(mPlaylistData.get_id());
//            mAudios = DBFloatPlayListDetailDaoHelp.queryBuilder(getThis(),mPlaylistData.getId()+"");
//            datas= changeData(mAudios);
//            if(adapter==null){
//                adapter = new PlayDetalAdapter(getThis(),datas, getSupportFragmentManager(),true,type);
//                footerListView.setAdapter(adapter);
//            }
//
//            if (datas.size()>0){
//                Glide.with(PlayDeatalActivity.this).load( datas.get(0).getPic()).transform(new GlideCornerTransform(PlayDeatalActivity.this,8)).dontAnimate().into(iv_playimg);
//            }else {
//                Glide.with(PlayDeatalActivity.this).load(R.drawable.icon_iv_fail).transform(new GlideCornerTransform(PlayDeatalActivity.this,8)).dontAnimate().into(iv_playimg);
//            }

        }
        EventBus.getDefault().register(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void xxxx(CollectMoreAudio collectSingleAudio){
        if (SPUtils.isLogined()){
            if (adapter != null){
                adapter.notifyCollectByIds(collectSingleAudio.getJson());
            }
        }else{
            if (adapter != null){
                adapter.notifyCollect();
            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void xxxx(CollectSingleAudio collectSingleAudio){
        if (SPUtils.isLogined()){
            if (adapter != null){
                adapter.notifyCollectById(collectSingleAudio.getAudioId());
            }
        }else {
            if (adapter != null) {
                adapter.notifyCollect();
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (isTaskRoot()){
            finish();
            Intent intent = new Intent(this,SplashActivity.class);
            startActivity(intent);
        }else {
            super.onBackPressed();
        }
    }

    private List<PlaydeatalData.DataBean.ListBean> changeData(List<DBFloatPlayListDetail> localData){
        List<PlaydeatalData.DataBean.ListBean> listBeans=new ArrayList<>();
        for (int i=0; i<localData.size();i++){
            PlaydeatalData.DataBean.ListBean bean=new PlaydeatalData.DataBean.ListBean();
            bean.setId(localData.get(i).getAudio_id());
            bean.setCover_id(localData.get(i).getAudio_cover_id());
//            listBeans.get(i).set(localData.get(i).getAudio_ps();
            bean.setTitle(localData.get(i).getAudio_title());
            bean.setUrl(localData.get(i).getAudio_url());
            bean.setPic(localData.get(i).getAudio_ps());
            listBeans.add(bean);
        }
        return  listBeans;
    }

    private void initData() {
        final TreeMap<String, String> list=new TreeMap<>();
//        list.put("cover_id",getIntent().getStringExtra(LIST_ID));
        if (!TextUtils.isEmpty(uid)){
            list.put("uid",uid);
        }
        list.put("member_cover_id",getIntent().getStringExtra(LIST_ID));
        list.put("page",String.valueOf(page));
        list.put("pagesize","10");

        HttpClient.post(HttpConfig.USER_COVER_DETAIL, list, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, String response) {
                Gson gson = new Gson();
//                String data = response.body().string();
                playdeatalData = gson.fromJson(response, PlaydeatalData.class);
//                Log.e("onResponse:====1== ",response.body().string());
//                Log.e("onResponse:===3333=== ",playdeatalData.getData().getList().size()+"");
                listBean= playdeatalData.getData().getList();
                for (int j = 0;j<listBean.size();j++){
                    listBean.get(j).setPic(playdeatalData.getData().getInfo().getPic());
                    listBean.get(j).setCover_name(playdeatalData.getData().getInfo().getName());
                    listBean.get(j).setCategory_id(playdeatalData.getData().getInfo().getCategory_id());
                    if (isManager){
                        if (isSelectAll){

                            listBean.get(j).setIsSelect(2);
                        }else {
                            listBean.get(j).setIsSelect(1);
                        }
                    }
                    datas.add(listBean.get(j));
                }
                sharedata=playdeatalData.getData().getShare();
                Message msg = new Message();
                msg.what = UPDATE;
                mHandler.sendMessage(msg);
            }
        });
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case UPDATE:

                    PlaydeatalData.DataBean.CoverInfoBean info= playdeatalData.getData().getInfo();
                    tv_header_title.setText(info.getName());
                    String uri=info.getPic();
                    if (!isFinishing()) {
                        try {
                            RequestOptions options = new RequestOptions();
                            options.transform(new GlideCornerTransform(getContext(), 8)).dontAnimate();
                            Glide.with(getContext()).load(TextUtils.isEmpty(uri) ? R.drawable.icon_iv_fail : uri).apply(options).into(iv_playimg);
                        }catch (Exception e){

                        }
                    }else{
                    }
//                    listBean.add(listBean.get(0));
//                    Log.e("onResponse:===111=== ",datas.+"");
                    if (adapter==null){
                        adapter = new PlayDetalAdapter(getContext(),datas , getSupportFragmentManager(),ISBODAN,type,pushtitle);
                        footerListView.setAdapter(adapter);
                    }else {
                        adapter.notifyDataSetChanged();
                    }
//                    adapter = new PlayDetalAdapter(PlayDeatalActivity.this,listBean , getSupportFragmentManager(),ISBODAN);
//                    footerListView.setAdapter(adapter);
//                    adapter.addList(listBean);
//                    footerListView.setAdapter(adapter);
                    if (listBean.size()<=0){
                        footerListView.getFooterView().setVisibility(View.GONE);
                        footerListView.setResult(FooterListView.ActionResult.NO_MORE);
                        isLoadAll = true;
                    }
                    break;
            }

        }
    };

    @Subscribe (threadMode = ThreadMode.MAIN)//(sticky = true)
    public void onEvent(AudioCompleted data){
        if(datas!=null&&adapter!=null){
            for (int j = 0;j<datas.size();j++){
                datas.get(j).setCheak(false);
                datas.get(j).setPause(false);
            }
            adapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        footerListView = (FooterListView) findViewById(R.id.flv_play);
        vHead = View.inflate(this, R.layout.header_playdetal, null);

        ll_header = (LinearLayout) vHead.findViewById(R.id.ll_header);
//        ll_play_all = (LinearLayout) vHead.findViewById(R.id.ll_play_all);
        ll_share = (LinearLayout) vHead.findViewById(R.id.ll_share);
        ll_share.setOnClickListener(this);
        tv_desc = (TextView) vHead.findViewById(R.id.tv_desc);
        iv_playimg = (ImageView) vHead.findViewById(R.id.iv_playimg);
        tv_header_title = (TextView) vHead.findViewById(R.id.tv_header_title);
        //开始管理
        ll_start_manager = (LinearLayout) vHead.findViewById(R.id.ll_start_manager);
        ll_start_manager.setOnClickListener(this);
        tv_header_titlename= (TextView) findViewById(R.id.tv_header_titlename);
        ll_back= (LinearLayout) findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);
        tv_select_all = (TextView) findViewById(R.id.tv_select_all);
        tv_select_all.setVisibility(View.GONE);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_select_all.setOnClickListener(this);
        ll_manager = (LinearLayout) findViewById(R.id.ll_manager);
        ll_manager.setVisibility(View.GONE);
//        rl_cover_delete = (RelativeLayout) findViewById(R.id.rl_cover_delete);
        rl_cover_add = (RelativeLayout) findViewById(R.id.rl_cover_add);
//        rl_cover_delete.setOnClickListener(this);
        rl_cover_add.setOnClickListener(this);

        if (ISBODAN) {
            tv_header_titlename.setText("我的播单");
            tv_desc.setVisibility(View.GONE);
            ll_share.setVisibility(View.GONE);
        } else {
            tv_header_titlename.setText("播单");
        }

        if (!TextUtils.isEmpty(nickname)){
            tv_desc.setText(nickname+"收藏的播单");
        }

        footerListView.addHeaderView(ll_header);

        footerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i==0||i==datas.size()+1)return;

                if (isManager){
                    selectItem(i-1);
                    return;
                }

                if (datas.get(i-1).isCheak()){
                    datas.get(i-1).setCheak(false);
                    datas.get(i-1).setPause(true);
                }else{
                    if (!datas.get(i-1).isPause()){
                        if (TextUtils.equals("push",type)){
                            ClickLog.clickLog(ClickLog.p_bag_feed_play,datas.get(i-1).getCover_id(),
                                    datas.get(i-1).getId());
                        }else if (TextUtils.equals("search",type)) {
                            ClickLog.clickLog(ClickLog.s_bag_feed_play,datas.get(i-1).getCover_id(),
                                    datas.get(i-1).getId());
                        }else {
                            ClickLog.clickLog(ClickLog.bag_feed_play, datas.get(i - 1).getCover_id(),
                                    datas.get(i - 1).getId());
                        }
                    }

                    for (int j = 0;j<datas.size();j++){
                        datas.get(j).setCheak(false);
                        datas.get(j).setPause(false);
                    }
                    datas.get(i-1).setCheak(true);
                }

                adapter.notifyDataSetChanged();

                EventBus.getDefault().post(new PlayerServiceData(PLAY,datas.get(i-1).getUrl()));

                //网络播单
                if (TextUtils.equals("push",type)){
                    MobclickAgent.onEvent(getThis().getApplicationContext(), MobClickHelper.p_BAG_FEED_PLAY, datas.get(i - 1).getTitle()+"-"+pushtitle+"-"+SPUtils.getLoginUid());
                    HashMap<String, Object> temp = new HashMap<>();
                    temp.put("网络播单", datas.get(i - 1).getTitle().toString());
                    ZhugeSDK.getInstance().track(getThis().getApplicationContext(), MobClickHelper.p_BAG_FEED_PLAY, temp);
                }else if (TextUtils.equals("search",type)){
                    MobclickAgent.onEvent(getThis().getApplicationContext(), MobClickHelper.s_BAG_FEED_PLAY, datas.get(i - 1).getTitle());
                    HashMap<String, Object> temp = new HashMap<>();
                    temp.put("网络播单", datas.get(i - 1).getTitle().toString());
                    ZhugeSDK.getInstance().track(getThis().getApplicationContext(),MobClickHelper.s_BAG_FEED_PLAY, temp);
                }else {
                    MobclickAgent.onEvent(getThis().getApplicationContext(), MobClickHelper.BAG_FEED_PLAY, datas.get(i - 1).getTitle());
                    HashMap<String, Object> temp = new HashMap<>();
                    temp.put("网络播单", datas.get(i - 1).getTitle().toString());
                    ZhugeSDK.getInstance().track(getThis().getApplicationContext(), MobClickHelper.BAG_FEED_PLAY, temp);
                }
            }
        });

        if (!ISBODAN){

            footerListView.setOnLastItemVisiableListener(new FooterListView.LastItemVisiableListener() {
                @Override
                public void onLastItemVisiable() {
                    page++;
                    initData();
                }
            });
        }else {
            footerListView.getFooterView().setVisibility(View.GONE);
        }

//        Log.e("onResponse:====2== ",playdeatalData.getData().getList().size()+"");



    }

    //界面退出的时候统一删除，因为单一删除会修改播单的id
    private void delAudio4List(String playlistID,List<String> audioDelList){
//        FloatPlayListData data = DBHelper.getInstance(getThis()).queryListDataByID(playlistID);
        DBFloatPlaylist data = DBFloatPlayListDaoHelp.queryBuilderById(getThis(),playlistID);
        if(data!=null){
            ArrayList<String> listAudioIds = new ArrayList<>();
            String[] audios = data.getList_audio().split(",");
            for(String audioId : audios){
                if (!TextUtils.isEmpty(audioId)) {
                    listAudioIds.add(audioId);
                }
            }

            for(String delId : audioDelList){
                listAudioIds = removeId(listAudioIds,delId);
            }

            StringBuffer result = new StringBuffer();
            for(String resultId : listAudioIds){
                result.append(resultId);
                result.append(",");
            }

            String newAudios = "";
            if(result!=null&&result.length()>0){
                newAudios = result.substring(0,result.length()-1);
            }
            data.setList_audio(newAudios);
            data.setList_count(String.valueOf(Integer.parseInt(data.getList_count())-audioDelList.size()));

            //FIXME 此方法会修改播单的_id
//            DBHelper.getInstance(getThis()).delPlayList(playlistID);
//            DBHelper.getInstance(getThis()).addList(data);
            DBFloatPlayListDaoHelp.update(getThis(),data);

            EventBus.getDefault().post(new NotifyMyPlaylist("notify"));
        }
    }

    private ArrayList<String> removeId(ArrayList<String> list,String id){
        ArrayList<String> result = new ArrayList<>();
        for(String temp : list){
            if(temp.equals(id)){
                continue;
            }
            result.add(temp);
        }
        return result;
    }

    private ArrayList<String> delAudioIds = new ArrayList<>();
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(NotifyPlayDetal messageEvent) {
        if (messageEvent.getMessage()=="share"){
            messageEvent.getListBean();
            new CopyShareFragment().show(getSupportFragmentManager(), "Dialog");
        }else if (messageEvent.getMessage()=="remove"){//这里需要加入删除数据库的操作
            for (int i=0;i<datas.size();i++){

                if (datas.get(i).getId().equals(messageEvent.getListBean().getId())){
//                    Toast.makeText(getThis(),datas.get(i).getId(),Toast.LENGTH_SHORT).show();
                    datas.remove(i);
                    adapter.notifyDataSetChanged();//TODO
//                    DBHelper.getInstance(getThis()).delAudios();
//                    adapter.setList(listBean);
//                    Toast.makeText(getThis(),"删除成功",Toast.LENGTH_SHORT).show();
                    delAudioIds.add(messageEvent.getListBean().getId());
                    showToast("移除成功");
                }
            }

        }else {//
            RecommendFeed.DataBean.ListBean bean= new RecommendFeed.DataBean.ListBean();
            for (int i = 0; i < datas.size(); i++) {
                if (messageEvent.getListBean().getId().equals(datas.get(i).getId())){
                    PlaydeatalData.DataBean.ListBean mbean =datas.get(i);

                    bean.setDuration(mbean.getDuration());
                    bean.setCategory_id(mbean.getCover_id());
                    bean.setId(mbean.getId());
                    bean.setCollect(mbean.getCollect());
                    bean.setShare(mbean.getShare());
                    bean.setStar(mbean.getStar());
                    bean.setTitle(mbean.getTitle());
                    bean.setUrl(mbean.getUrl());
                    bean.setCover_pic(mbean.getPic());
                    if (SPUtils.isLogined()){
                        Intent addIntent = new Intent(getThis(), Add2NetListActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(AUDIO_ID, bean);
                        addIntent.putExtras(bundle);
                        getThis().startActivity(addIntent);
                    }else {
                        Intent addIntent = new Intent(getThis(), Add2ListActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(AUDIO_ID, bean);
                        addIntent.putExtras(bundle);
                        getThis().startActivity(addIntent);
                    }
                    break;
                }
            }

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_share:
                if (sharedata != null) {
                    new CustomBottomSheetDialogFragment(sharedata, nickname).show(getSupportFragmentManager(), "Dialog");//展示分享弹窗
                }else{
                    showToast("暂不支持分享");
                }
                break;
            case R.id.ll_back:
                if (isManager){
                    isManager = false;
                    ll_start_manager.setVisibility(View.VISIBLE);
                    iv_back.setVisibility(View.VISIBLE);
                    tv_cancel.setVisibility(View.GONE);
                    tv_select_all.setVisibility(View.GONE);
                    ll_manager.setVisibility(View.GONE);
                    tv_select_all.setText("全选");
                    //取消
                    cancelManager();
                    //排序更新
//                    refreshSort();
                }else{
                    finish();
                }
//                if(ISBODAN){
//                    delAudio4List(mPlaylistData.getId()+"",delAudioIds);
//                }
//                finish();
                break;
            case R.id.ll_start_manager:
                if (datas.size()==0){
                    break;
                }
                isManager = true;
//                new CustomBottomSheetDialogFragment(sharedata).show(getSupportFragmentManager(), "Dialog");//展示分享弹窗
//                Toast.makeText(PlayDetailMineActivity.this,"管理",Toast.LENGTH_LONG).show();
                ll_start_manager.setVisibility(View.GONE);
                iv_back.setVisibility(View.GONE);
                tv_cancel.setVisibility(View.VISIBLE);
                tv_select_all.setVisibility(View.VISIBLE);
                ll_manager.setVisibility(View.VISIBLE);
                if (!isLoadAll){
                    loadAll();
                }else{
                    startManager();
                }
//                startManager();
                break;
            case R.id.tv_select_all:
                //全选
                if ("全选".equals(tv_select_all.getText())){
                    isSelectAll = true;
                    selectAll();
                    tv_select_all.setText("取消\n全选");
                }else{
                    isSelectAll = false;
                    cancelSelectAll();
                    tv_select_all.setText("全选");
                }
                break;

            case R.id.rl_cover_add:
                addCover();
                break;

        }
    }


    private void addCover() {
        if (adapter != null){
            ArrayList<PlaydeatalData.DataBean.ListBean> select = adapter.getSelect();
            if (select.size()> 0){
//                delAudio4List(mPlaylistData.getId()+"",select2);
                if (SPUtils.isLogined()){
                    Intent addIntent = new Intent(getThis(), Add2NetListActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("net_cover", select);
                    addIntent.putExtras(bundle);
                    getThis().startActivity(addIntent);
                }else {
                    Intent addIntent = new Intent(getThis(), Add2ListActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("net_cover", select);
                    addIntent.putExtras(bundle);
                    getThis().startActivity(addIntent);
                }
            }else{
                showToast("请选择要收藏的音频");
            }
        }
    }


    private void selectItem(int position){
        if (adapter != null){
            adapter.selectItem(position);
        }
    }

    private void cancelSelectAll(){
        if (adapter != null){
            adapter.cancelSelectAll();
        }
    }

    private void cancelManager(){
        if (adapter != null){
            adapter.cancelManager();
        }

    }

    private void startManager(){
        if (adapter != null){
            adapter.startManager();
        }
    }

    private void selectAll(){
        if (adapter != null){
            adapter.selectAll();
        }
    }

    private void showBottomSheet(BottomSheetBehavior behavior) {
        if (behavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
            behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        } else {
            behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (mHandler != null){
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isManager){
                return true;
            }
            if(ISBODAN){
                delAudio4List(mPlaylistData.getId()+"",delAudioIds);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void loadAll() {
        final TreeMap<String, String> list=new TreeMap<>();
        list.put("member_cover_id",getIntent().getStringExtra(LIST_ID));
        list.put("pagesize","-1");
        if (!TextUtils.isEmpty(uid)){
            list.put("uid",uid);
        }
//        list.put("pagesize","20");

        HttpClient.post(HttpConfig.USER_COVER_DETAIL, list, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, String response) {
                Gson gson = new Gson();
                playdeatalData = gson.fromJson(response, PlaydeatalData.class);
//                Log.e("onResponse:====1== ",response.body().string());
//                Log.e("onResponse:===3333=== ",playdeatalData.getData().getList().size()+"");
                listBean= playdeatalData.getData().getList();
                for (int j = 0;j<listBean.size();j++){
                    listBean.get(j).setPic(playdeatalData.getData().getInfo().getPic());
                    listBean.get(j).setCover_name(playdeatalData.getData().getInfo().getName());
                    listBean.get(j).setCategory_id(playdeatalData.getData().getInfo().getCategory_id());
                    if (isManager){
                        if (isSelectAll){
                            listBean.get(j).setIsSelect(2);
                        }else {
                            listBean.get(j).setIsSelect(1);
                        }
                    }
                }
                datas.clear();
                datas.addAll(listBean);
                isLoadAll = true;
                sharedata=playdeatalData.getData().getShare();
//                Message msg = new Message();
//                msg.what = UPDATE;
//                mHandler.sendMessage(msg);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                        footerListView.setResult(FooterListView.ActionResult.NO_MORE);
                        startManager();
                    }
                });
            }
        });
    }
}

